package xcmg.device.web.purchase;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.dao.entity.purchase.PurchasePriceDO;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.purchase.PurchasePriceService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.util.ToolPrice;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping( "/purchasePrice" )
public class PurchasePriceController extends BasicController
{
	@Autowired
	private PurchasePriceService purchasePriceService;

	@ApiOperation( value = "列表查询" )
	@RequestMapping( value = "/list", method = RequestMethod.POST )
	@FastMappingInfo( needLogin = true )
	public ActionResult list( @RequestBody SearchCommonVO<PurchasePriceDO> condition ) throws Exception
	{
		PurchasePriceDO purchasePrice = condition.getFilters();
		purchasePrice.setOrgId( getOrgId() );
		condition.setFilters( purchasePrice );
		PageCommonVO<PurchasePriceDO> pageCommonVO = purchasePriceService.list( condition );

		return actionValueResult( pageCommonVO.getPageInfo() );
	}

	@ApiOperation( value = "新增采购价格")
	@RequestMapping( value = "/add", method = RequestMethod.POST )
	@FastMappingInfo( needLogin = true )
	public ActionResult add(@RequestBody PurchasePriceDO purchasePrice) throws Exception {
		try {
			PurchasePriceDO _purchasePrice = new PurchasePriceDO();
			_purchasePrice.setMaterialsNo(purchasePrice.getMaterialsNo());
			_purchasePrice.setDeleteFlag(0);
			_purchasePrice.setSupplierOrgId(purchasePrice.getSupplierOrgId());
			_purchasePrice.setOrgId(getOrgId());
			List<PurchasePriceDO> list = purchasePriceService.selectListByExact(_purchasePrice);
			if (null != list && list.size() > 0) {
				return new ActionResult(ErrorCode.Failure.getCode(), "该供应商与零件关系已存在！");
			} else {
				purchasePrice.setCreatorId(getUserId());
				purchasePrice.setCreatorName(getUserName());
				purchasePrice.setCreateTime(new Date());
				purchasePrice.setModifierId(getUserId());
				purchasePrice.setModifierName(getUserName());
				purchasePrice.setModifyTime(new Date());
				purchasePrice.setOrgId(getOrgId());
				purchasePrice.setDeleteFlag(0);
				purchasePrice.setPrice(ToolPrice.storagPriceHandle(purchasePrice.getPrice()));
				return actionResult(purchasePriceService.addPurchasePrice(purchasePrice));
			}
		} catch (Exception e) {
		    e.printStackTrace();
			return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
		}
	}

	@ApiOperation( value = "根据主键查询采购价格" )
	@RequestMapping( value = "/item", method = RequestMethod.GET )
	@FastMappingInfo( needLogin = true )
	public ActionResult item( String id )
	{
		try {
			PurchasePriceDO queryCond = new PurchasePriceDO();
			queryCond.setId(id);
			queryCond.setOrgId(getOrgId());
			return actionValueResult(purchasePriceService.itemForPurchasePrice(queryCond));
		} catch (Exception e) {
			e.printStackTrace();
			return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
		}

	}

	@ApiOperation( value = "修改采购价格" )
	@RequestMapping( value = "/update", method = RequestMethod.POST )
	@FastMappingInfo( needLogin = true )
	public ActionResult update( @RequestBody PurchasePriceDO purchasePrice ) throws Exception
	{
		try
		{
			purchasePrice.setModifierId( getUserId() );
			purchasePrice.setModifierName( getUserName() );
			purchasePrice.setModifyTime( new Date() );

			return actionResult( purchasePriceService.updatePurchasePrice( purchasePrice ) );
		}
		catch ( Exception e )
		{
			return new ActionResult( ErrorCode.Failure.getCode(), e.getMessage() );
		}
	}

	@ApiOperation( value = "删除采购价格" )
	@RequestMapping( value = "/delete", method = RequestMethod.POST )
	@FastMappingInfo( needLogin = true )
	public ActionResult delete( String id )
	{
		try
		{
			PurchasePriceDO purchasePrice = new PurchasePriceDO();
			purchasePrice.setId( id );
			purchasePrice.setDeleteFlag( 1);
			purchasePrice.setDeleteId( getUserId() );
			purchasePrice.setDeleteName( getUserName() );
			purchasePrice.setDeleteTime( new Date() );

			return actionResult( purchasePriceService.deletePurchasePrice( purchasePrice ) );
		}
		catch ( Exception e )
		{
			return new ActionResult( ErrorCode.Failure.getCode(), e.getMessage() );
		}
	}

	@ApiOperation( "批量删除采购价格" )
	@RequestMapping( value = "/batchDelete", method = RequestMethod.POST )
	public ActionResult remove( @RequestBody String[] ids )
	{
		try
		{
			List<PurchasePriceDO> priceList = new ArrayList<PurchasePriceDO>();
			for ( String id : ids )
			{
				PurchasePriceDO purchasePrice = new PurchasePriceDO();
				purchasePrice.setId( id );
				purchasePrice.setDeleteFlag( 1);
				purchasePrice.setDeleteId( getUserId() );
				purchasePrice.setDeleteName( getUserName() );
				purchasePrice.setDeleteTime( new Date() );
				priceList.add( purchasePrice );
			}

			return actionResult( purchasePriceService.batchDeletePurchasePrice( priceList ) );
		}
		catch ( Exception e )
		{
			return new ActionResult( ErrorCode.Failure.getCode(), e.getMessage() );
		}
	}

	@ApiOperation( value = "导入采购价格模板下载" )
	@RequestMapping( value = "/download", method = RequestMethod.POST )
	@FastMappingInfo( needLogin = true )
	public void download() throws Exception
	{
		purchasePriceService.download();
	}
	
	@ResponseBody
	@ApiOperation( value = "导入采购价格" )
	@RequestMapping( value = "/importBuyPlan", method = RequestMethod.POST)
	@FastMappingInfo( needLogin = true )
	public ActionResult importPurchasePrice( MultipartHttpServletRequest multipartRequest ) throws Exception
	{
		MultipartFile file = multipartRequest.getFile( multipartRequest.getFileNames().next() );
		String fileName = file.getOriginalFilename();
		FunctionResult<Object> result = new FunctionResult<>();
		if (!fileName.trim().endsWith( ".xls" ))
		{
			result.setCode( ErrorCode.ImportExcelEndWithXLS );
		}
		else
		{
			InputStream in = file.getInputStream();
			result = purchasePriceService.importPurchasePrice( in, getOrgId(), getUserId(), getUserName() );
		}
		return actionResult( result.getCode(), result.getValue() );
	}

}
